﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DataService.Core.Common
{
    public static class BatchHelper
    {
        /// <summary>
        /// 返回批次需要执行的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list"></param>
        /// <param name="num">默认每30条执行一次</param>
        /// <returns></returns>
        public static Dictionary<int, List<T>> ReturnBeatch<T>(IList<T> paraList, int num = 30)
        {
            if (paraList == null || paraList.Count == 0)
                return new Dictionary<int, List<T>>();
            List<T> tempList = new List<T>(paraList);
            Dictionary<int, List<T>> dic = new Dictionary<int, List<T>>();
            int beatchNumber = ReturnBeatchNum(tempList.Count, num);
            for (int i = 0; i < beatchNumber; i++)
            {
                var newlist = tempList.Take(num).ToList();
                dic[i] = newlist;
                tempList.RemoveRange(0, newlist.Count);//移除已经使用的集合
            }
            return dic;
        }
        private static int ReturnBeatchNum(int totalCount, int takeNumber)
        {
            int beatchNumber = 1;
            if (totalCount > takeNumber)
            {
                beatchNumber = totalCount / takeNumber;
                if (totalCount % takeNumber > 0)
                {
                    beatchNumber = beatchNumber + 1;
                }
            }
            return beatchNumber;
        }
    }
}
